#!/usr/bin/python
# -*- coding: utf-8 -*-

#############################################
#PROJECT : Download Tool
#FILE    : downImage.py
#USAGE   : Download Picture form some websit 
#CREATION: 20120610
#LICENSE : IIE
#$ID$    : Leon
#############################################

import urllib
import urllib2
from BeautifulSoup import BeautifulSoup
import os

if __name__ == '__main__':

	path = '/home/leon/p/hellokisses/'
	k = 867
	exitFlag = False
	while not exitFlag:
		URL = 'http://hellokisses.com/?page=' + str(k)
		print 'links: ', URL
		try:
			page = urllib2.urlopen(URL)
		except:
			print 'urllib2.urlopen error: ', URL
			break
		k += 1
		
		soup = BeautifulSoup(page, fromEncoding='gb18030')
		#print soup.prettify()

		img = soup.findAll('img')
		#print len(img)
		for i in img[:-2]:
			print i['src']
			dateName = i.parent['href'].split('/')
			print dateName
			print dateName[2], dateName[3]
			pathName = path + dateName[1] + '/' + dateName[2] + '/' + dateName[3][0] + '/'

			#以月份+文件名的第一个字母为父路径，如果不存在则新建
			if not os.path.exists(pathName):
				os.makedirs(pathName)

            #如果文件已经存在则跳过
			if os.path.isfile(pathName + dateName[3] + '.jpg'):
				print 'exist'
				continue
			urllib.urlretrieve(i['src'], pathName + dateName[3] + '.jpg')
			print 'done'

